package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.AddressBean;
import org.gangcai.mac.shop.bean.AddressItemBean;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

import static org.gangcai.mac.shop.constants.Constant.CODE_SUCC;

public class AddressListActivity extends MultiStatusActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.addAddressBtn)
    Button addAddressBtn;
    ArrayList<AddressItemBean> addressBeans = new ArrayList<>();
    private CommonAdapter<AddressItemBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "选择收货地址");
        ButterKnife.bind(this);
        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();
        loadData();

    }

    private void loadData() {
        multipleStatusView.showLoading();
        RetrofitManager.create().address_list(SPUtils.getInstance().getString("uid"))
                .compose(this.<AddressBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<AddressBean>(multipleStatusView) {
                    @Override
                    public void onSuccess(AddressBean value) {
                        if (value.getCode() == CODE_SUCC) {
                            if (value.getInfo() == null || value.getInfo().size() == 0) {
//                                AddAddressActivity.openForResult(100);
                            } else {
                                addressBeans.clear();
                                addressBeans.addAll(value.getInfo());
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                });

    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));

        adapter = new CommonAdapter<AddressItemBean>(this, R.layout.activity_add_address_recyclerview_item, addressBeans) {
            @Override
            protected void convert(ViewHolder holder, final AddressItemBean addressBean, final int position) {
                holder.setText(R.id.addressDetail, addressBean.getCity() + addressBean.getAddress());
                holder.setText(R.id.addressTell, addressBean.getMobile());
                holder.setText(R.id.addressName, addressBean.getName());
                holder.setOnClickListener(R.id.btnTop, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addressBeans.remove(position);
                        adapter.notifyDataSetChanged();
                        removeAddressItem(addressBean);
                    }
                });
                holder.setOnClickListener(R.id.addresContainer, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent data = new Intent();
                        data.putExtra("addressItem", addressBean);
                        setResult(RESULT_OK, data);
                        finish();
                    }
                });
            }

        };
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                AddressItemBean addressItemBean = addressBeans.get(position);
                Intent data = new Intent();
                data.putExtra("addressItem", addressItemBean);
                setResult(RESULT_OK, data);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    private void removeAddressItem(AddressItemBean addressBean) {
        RetrofitManager.create().address_del(addressBean.getId()).compose(this.<CommonResonseBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {

                    }
                });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100 && resultCode == RESULT_OK) {
            loadData();
           /* if (adapter.getItemCount() == 0) {
                setResult(RESULT_OK, data);
                finish();


            } else {
                AddressItemBean addressItem = data.getParcelableExtra("addressItem");
                addressBeans.add(addressItem);
                adapter.notifyDataSetChanged();
            }*/
        }

    }

    @Override
    public int getContentView() {
        return R.layout.activity_address_list;
    }

    @OnClick(R.id.addAddressBtn)
    public void onViewClicked() {
        AddAddressActivity.openForResult(100);
    }

    public static void open() {

    }

    /**
     * 跳转到当前界面
     *
     * @param requestCode
     */
    public static void openForResult(int requestCode) {
        ActivityUtils.
                getTopActivity().
                startActivityForResult(new Intent(ActivityUtils.getTopActivity(), AddressListActivity.class), requestCode);
    }
}
